package com.cskaoyan.mall.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.order.dto.OrderInfoDTO;
import com.cskaoyan.mall.order.dto.OrderTradeDTO;
import com.cskaoyan.mall.order.query.OrderInfoParam;
import com.cskaoyan.mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order/auth")
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping("/trade")
    public Result trade(HttpServletRequest httpServletRequest){
        String userId = httpServletRequest.getHeader("userId");
        OrderTradeDTO orderTradeDTO=orderService.getTradeInfo(userId);
        return Result.ok(orderTradeDTO);
    }

    @PostMapping ("/submitOrder")
    public Result submitOrder(@RequestBody OrderInfoParam orderInfoParam, HttpServletRequest httpServletRequest){
        String userId = httpServletRequest.getHeader("userId");
        Long orderId=orderService.submitOrder(orderInfoParam,userId);
        return Result.ok(orderId);

    }

    @GetMapping("{page}/{limit}")
    public Result getList(@PathVariable Long limit, @PathVariable Long page, HttpServletRequest httpServletRequest){
        String userId = httpServletRequest.getHeader("userId");
        IPage<OrderInfoDTO> infoDTOIPage = orderService.getPage(new Page<>(page,limit), userId);
        return Result.ok(infoDTOIPage);
    }

}
